/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLLER;

import MODEL.DAO.UsersDao;
import MODEL.POJO.Users;
import java.io.IOException;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tri ben tre
 */
@WebServlet(name = "RegisterController", urlPatterns = {"/RegisterController"})
public class RegisterController extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        response.setContentType("text/html;charset=UTF-8");
        //PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            String action = (String)request.getParameter("action");
            if (action != null) {
                int identify = Integer.parseInt(request.getParameter("identify"));
                if (action.equals("checkUser")) {
                    List<Users> ds = UsersDao.checkExistIdentifyCard(identify);
                    if(ds.size() > 0){
                        session.setAttribute("user", ds.get(0));
                        String message = "1";
                        response.setContentType("text/javascript");
                        response.getOutputStream().print(message);
                        response.flushBuffer();
                        return;
                    }
                }else if (action.equals("register")) {
                    String birthday = request.getParameter("birthday");
                    DateFormat formatter = new SimpleDateFormat("yy-MM-dd");
                    Date birth = (Date)formatter.parse(birthday);
                    String address = request.getParameter("address"); 
                    String fullname = request.getParameter("fullname"); 
                    Users u = new Users();
                    u.setUserIdentityCard(identify);
                    u.setUserFullname(fullname);
                    u.setUserBirthday(birth);
                    u.setUserAddress(address);
                    boolean kq = UsersDao.addUser(u);
                    if(kq == true)
                        session.setAttribute("user", u);
                }
            }
        } finally {            
            //out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
